<template>
  <div v-if="visible" class="global-loading">
    <div class="loading-content">
      <el-progress type="circle" :percentage="percentage" :status="status">
        <template #default>
          <div class="loading-text">
            <div>{{ text }}</div>
            <div class="sub-text" v-if="subText">{{ subText }}</div>
          </div>
        </template>
      </el-progress>
    </div>
  </div>
</template>

<script>
export default {
  name: 'GlobalLoading',
  data() {
    return {
      visible: false,
      percentage: 0,
      status: '',
      text: '加载中...',
      subText: '',
      timer: null
    }
  },
  methods: {
    show(options = {}) {
      this.visible = true
      this.percentage = options.percentage || 0
      this.status = options.status || ''
      this.text = options.text || '加载中...'
      this.subText = options.subText || ''
      
      // 自动更新进度
      if (options.autoProgress) {
        this.startProgress()
      }
    },
    hide() {
      this.visible = false
      this.stopProgress()
    },
    startProgress() {
      this.stopProgress()
      this.timer = setInterval(() => {
        if (this.percentage < 90) {
          this.percentage += Math.random() * 10
        }
      }, 1000)
    },
    stopProgress() {
      if (this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
    },
    setProgress(percentage, status) {
      this.percentage = percentage
      if (status) {
        this.status = status
      }
    }
  },
  beforeDestroy() {
    this.stopProgress()
  }
}
</script>

<style lang="scss" scoped>
.global-loading {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;

  .loading-content {
    text-align: center;
    padding: 20px;
    border-radius: 8px;
    background: white;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  }

  .loading-text {
    margin-top: 10px;
    font-size: 16px;
    color: #333;

    .sub-text {
      margin-top: 5px;
      font-size: 12px;
      color: #999;
    }
  }
}
</style> 